package b3.widget.match
{
	import b3.net.*;
	import b3.model.*;
	import b3.widget.*;
	import b3.util.*;
	
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.filters.*;
	import flash.text.*;
	import flash.ui.Mouse;

	public class MatchItem extends Sprite
	{
		private const RANK_COLOR:Array = [0xFF0000, 0xA0A000, 0x00A000, 0x000000];
		private const RANK_NAME:Array = ["st", "nd", "rd", ""];
		private var rankInfo:RankInfo;
		
		public function MatchItem(rankInfo:RankInfo)
		{
			super();
			this.rankInfo = rankInfo;
			//背景框
			var MatchItemBgr:Class = ResourceHelper.getInstance().getClassByName("MatchItemBgr");
			addChild(new MatchItemBgr());
			//排名
			var rankingText:TextField = new TextField();
			rankingText.selectable = false;
        	rankingText.autoSize = TextFieldAutoSize.LEFT;
        	var colorIndex:int = Math.min(rankInfo.ranking, 3);
			rankingText.defaultTextFormat = new TextFormat("微软雅黑", 14, RANK_COLOR[colorIndex], true);
			rankingText.filters = [new GlowFilter(0xFFFFFF, 1, 1.3, 1.3, 100, BitmapFilterQuality.HIGH)];
			rankingText.x = 20;
			rankingText.y = 5;
			rankingText.text = (rankInfo.ranking + 1).toString() + RANK_NAME[colorIndex];
			addChild(rankingText);
			//用户名字
			var playerNameText:TextField = new TextField();
			playerNameText.selectable = false;
        	playerNameText.autoSize = TextFieldAutoSize.CENTER;
			playerNameText.defaultTextFormat = new TextFormat("微软雅黑", 14, RANK_COLOR[colorIndex], true);
			playerNameText.filters = [new GlowFilter(0xFFFFFF, 1, 1.3, 1.3, 100, BitmapFilterQuality.HIGH)];
			playerNameText.x = 115;
			playerNameText.y = 5;
			playerNameText.text = rankInfo.name.substr(0,4);
			addChild(playerNameText);
			//数值
			var numText:TextField = new TextField();
			numText.selectable = false;
        	numText.autoSize = TextFieldAutoSize.CENTER;
			numText.defaultTextFormat = new TextFormat("微软雅黑", 14, RANK_COLOR[colorIndex], true);
			numText.filters = [new GlowFilter(0xFFFFFF, 1, 1.3, 1.3, 100, BitmapFilterQuality.HIGH)];
			numText.x = 210;
			numText.y = 5;
			numText.text = rankInfo.score.toString();
			addChild(numText);
			//建设时间
			var buildDateText:TextField = new TextField();
			buildDateText.selectable = false;
        	buildDateText.autoSize = TextFieldAutoSize.LEFT;
			buildDateText.defaultTextFormat = new TextFormat("微软雅黑", 14, RANK_COLOR[colorIndex], true);
			buildDateText.filters = [new GlowFilter(0xFFFFFF, 1, 1.3, 1.3, 100, BitmapFilterQuality.HIGH)];
			buildDateText.x = 270;
			buildDateText.y = 5;
			buildDateText.text = rankInfo.buildDate.substr(5, 11);
			addChild(buildDateText);
			
			this.addEventListener(MouseEvent.CLICK, onMouseClick);
			this.addEventListener(MouseEvent.MOUSE_OVER,onMouseOver);
			this.addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
		}
		
//		public function getLevel(exp:int):Array
//		{
//			var level:int = 0;
//			while (level < Config.levelConfigs.length && exp >= Config.levelConfigs[level].experience)
//			{
//				exp -= Config.levelConfigs[level].experience;
//				level++;
//			}
//			level = level >= Config.levelConfigs.length ? Config.levelConfigs.length - 1 : level;
//			var rate:int = Math.floor(exp * 100 / Config.levelConfigs[level].experience);
//			return [level + 1, rate];
//		}
		
		private function onMouseClick(event:MouseEvent):void
		{
//			ServerHelper.getInstance().getPlayerInfo(this.rankingInfo.playerId);
//			ServerHelper.getInstance().addEventListener(ServerEvent.GET_PLAYER_INFO, onShowTankInfoComplete);
		}
//		
//		private function onShowTankInfoComplete(event:ServerEvent):void
//		{
//			ServerHelper.getInstance().removeEventListener(ServerEvent.GET_PLAYER_INFO, onShowTankInfoComplete);
//			TankDialog.getInstance().fresh(this.rankingInfo.tankId);
//			WidgetManager.getInstance().popOut([TankDialog.getInstance()], false, true);
//		}
//		
		private function onMouseOver(event:MouseEvent):void
		{
			Mouse.cursor = "button";
			this.filters = [new ColorMatrixFilter(BFont.brightFilter)];
		}
		
		private function onMouseOut(event:MouseEvent):void
		{
			Mouse.cursor = "auto";
			this.filters = null;
		}
	}
}